﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Budgeter.Models;
using Budgeter.ViewModels.Budget;
using Budgeter.Extensions;

namespace Budgeter.Controllers
{
    public class BudgetController : ControllerBase
    {

        //
        // GET: /Budget/Create

        public ActionResult Create()
        {
            var model = new Budget();

            var loader = new Budgeter.ViewModels.Budget.CreateOrEdit.Loader();
            var viewModel = loader.Load(BudgeterContext, model);

            return View("CreateOrEdit", viewModel);
        }

        //
        // POST: /Budget/Create

        [HttpPost]
        public ActionResult Create(Budgeter.ViewModels.Budget.CreateOrEdit.ViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var saver = new Budgeter.ViewModels.Budget.CreateOrEdit.Saver();
                Budget model = saver.Save(BudgeterContext, viewModel);

                return RedirectToAction("AfterCreate", new { id = model.ReceiptNumber });
            }

            var loader = new Budgeter.ViewModels.Budget.CreateOrEdit.Loader();
            loader.PopulateDataForView(BudgeterContext, viewModel);

            return View("CreateOrEdit", viewModel);
        }

        //
        // GET: /Budget/Edit/555555

        public ActionResult Edit(string id)
        {
            int receiptNumber = Receipt.ParseReceiptNumber(id);
            Budget model = BudgeterContext.Budgets.FindByReceiptNumber(receiptNumber);

            var loader = new Budgeter.ViewModels.Budget.CreateOrEdit.Loader();
            var viewModel = loader.Load(BudgeterContext, model);

            return View("CreateOrEdit", viewModel);
        }

        //
        // POST: /Budget/Edit/

        [HttpPost]
        public ActionResult Edit(Budgeter.ViewModels.Budget.CreateOrEdit.ViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var saver = new Budgeter.ViewModels.Budget.CreateOrEdit.Saver();
                saver.Save(BudgeterContext, viewModel);
                // #837: stay on budget screen when saved
            }

            // TODO: splash message: your budget has been saved!

            var loader = new Budgeter.ViewModels.Budget.CreateOrEdit.Loader();
            loader.PopulateDataForView(BudgeterContext, viewModel);
            return View("CreateOrEdit", viewModel);
        }

        //
        // GET: /Budget/Delete/555555

        public ActionResult Delete(string id)
        {
            int receiptNumber = Receipt.ParseReceiptNumber(id);
            Budget budget = BudgeterContext.Budgets.FindByReceiptNumber(receiptNumber);
            return View(budget);
        }

        //
        // POST: /Budget/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            int receiptNumber = Receipt.ParseReceiptNumber(id);
            Budget budget = BudgeterContext.Budgets.FindByReceiptNumber(receiptNumber);
            BudgeterContext.Budgets.Remove(budget);
            BudgeterContext.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult AfterCreate(string id)
        {
            int receiptNumber = Receipt.ParseReceiptNumber(id);
            Budget model = BudgeterContext.Budgets.FindByReceiptNumber(receiptNumber);

            var loader = new Budgeter.ViewModels.Budget.AfterCreate.Loader();
            var viewModel = loader.Load(BudgeterContext, model);

            return View(viewModel);
        }

        public ActionResult Search(string id)
        {
            int receiptNumber = Receipt.ParseReceiptNumber(id);
            Budget model = BudgeterContext.Budgets.FirstOrDefault(x => x.ReceiptNumber == receiptNumber);
            if (model == null)
                return RedirectToAction("");
            else
                return RedirectToAction("Edit", new { id = id });
        }

    }
}